package com.ruoyi.buss.grid.cj.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.buss.active.domain.DataKhhdImport;
import com.ruoyi.buss.active.mapper.DataKhhdImportMapper;
import com.ruoyi.buss.customer.domain.DataCustomerRecord;
import com.ruoyi.buss.customer.domain.DataWgJtcy;
import com.ruoyi.buss.customer.mapper.DataCustomerRecordMapper;
import com.ruoyi.buss.customer.mapper.DataWgJtcyMapper;
import com.ruoyi.buss.grade.domain.DataCustomQuery;
import com.ruoyi.buss.grade.mapper.DataCustomQueryMapper;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcj;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcjHjbh;
import com.ruoyi.buss.grid.cj.domain.DataWgWgcjVillage;
import com.ruoyi.buss.grid.cj.mapper.DataWgWgcjHjbhMapper;
import com.ruoyi.buss.grid.cj.mapper.DataWgWgcjMapper;
import com.ruoyi.buss.grid.cj.mapper.DataWgWgcjVillageMapper;
import com.ruoyi.buss.grid.cj.service.IDataWgWgcjService;
import com.ruoyi.buss.system.domain.SysBusparams;
import com.ruoyi.buss.system.mapper.SysBusparamsMapper;
import com.ruoyi.buss.task.domain.DataPlanTaskRelation;
import com.ruoyi.buss.task.domain.DataTaskInfo;
import com.ruoyi.buss.task.domain.DataUserTaskRelation;
import com.ruoyi.buss.task.domain.UserTaskRelationDO;
import com.ruoyi.buss.task.mapper.DataPlanTaskRelationMapper;
import com.ruoyi.buss.task.mapper.DataUserTaskRelationMapper;
import com.ruoyi.buss.task.service.IDataTaskInfoService;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.common.core.enums.ErrorCode;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 网格村居Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class DataWgWgcjServiceImpl implements IDataWgWgcjService {
	private Logger logger = LoggerFactory.getLogger(DataWgWgcjServiceImpl.class);

	@Autowired
	private DataWgWgcjMapper dataWgWgcjMapper;
    @Autowired
    private DataPlanTaskRelationMapper dataPlanTaskRelationMapper;
	@Autowired
    private DataUserTaskRelationMapper dataUserTaskRelationMapper;
	@Autowired
	private SysBusparamsMapper sysBusparamsMapper;
	@Autowired
	private DataCustomerRecordMapper dataCustomerRecordMapper;
	@Autowired
	private DataWgWgcjVillageMapper dataWgWgcjVillageMapper;
	@Autowired
	private DataWgWgcjHjbhMapper dataWgWgcjHjbhMapper;
	
	@Autowired
	private DataWgJtcyMapper dataWgJtcyMapper;
	
	@Autowired
	private DataCustomQueryMapper dataCustomQueryMapper;
	@Autowired
	private DataKhhdImportMapper dataKhhdImportMapper;
    @Autowired
    private IDataTaskInfoService dataTaskInfoService;
    
	private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(30);

	public String ifCheck(String Check) {
		SysBusparams sysBusparams = sysBusparamsMapper.selectSysBusparamsById(Check);
		String ifCheck = sysBusparams.getValue();
		return ifCheck;
	}
	
	public List<DataWgWgcjVillage> queryGridCJList(DataWgWgcjVillage condition, LoginUser userInfo,String ifCheck) {

		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		List<DataWgWgcjVillage> doList = new ArrayList<DataWgWgcjVillage>();
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office") || "1".equals(ifCheck)) {
			// 如果接口传输orgId，说明管理员查询该机构信息，优先级高
			if (StringUtils.isNotEmpty(condition.getOrgId())) {
				doList = dataWgWgcjVillageMapper.selectDataWgWgcjVillageList(condition);
			} else {
				// 管理员角色或者总行人员，查询所有客户
				doList = dataWgWgcjVillageMapper.listByRoot();
			}
		} else if (userInfo.getRoles().contains("zhhz")) {
			// 支行行长，查询所有本支行客户
			condition.setOrgId(orgId);
			doList = dataWgWgcjVillageMapper.selectDataWgWgcjVillageList(condition);
		} else if (userInfo.getRoles().contains("common")) {
			// 支行客户经理或者柜员，查询所属自己的客户
			condition.setOrgId(orgId);
			condition.setManagerId(userId);
			doList = dataWgWgcjVillageMapper.selectDataWgWgcjVillageList(condition);
		} else {
			throw new CustomException("没有权限进行该操作!");
		}
		return doList;
	}

	public DataWgWgcjVillage getGridSurvey(DataWgWgcjVillage dataWgWgcjVillage, LoginUser userInfo,String ifCheck) {

		DataWgWgcjVillage gridSurvey = new DataWgWgcjVillage();
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office") || "1".equals(ifCheck)) {
			// 管理员角色或者总行人员，查询所有客户
			if (StringUtils.isNotEmpty(dataWgWgcjVillage.getOrgId())) {
				gridSurvey.setOrgId(dataWgWgcjVillage.getOrgId());
				gridSurvey.setVillage(dataWgWgcjVillage.getVillage());
			}
		} else if (userInfo.getRoles().contains("zhhz")) {
			// 支行行长，查询所有本支行客户
			gridSurvey.setOrgId(orgId);
			gridSurvey.setVillage(dataWgWgcjVillage.getVillage());
		} else if (userInfo.getRoles().contains("common")) {
			// 支行客户经理或者柜员，查询所属自己的客户
			gridSurvey.setManagerId(userId);
			gridSurvey.setOrgId(orgId);
			gridSurvey.setVillage(dataWgWgcjVillage.getVillage());
		} else {
			throw new CustomException("没有权限进行该操作!");
		}
		gridSurvey = dataWgWgcjVillageMapper.getGridSurvey(gridSurvey);
		return gridSurvey;
	}
	
	
	public List<DataWgWgcjHjbh> villageList(DataWgWgcjHjbh condition) {
		List<DataWgWgcjHjbh> doList = new ArrayList<DataWgWgcjHjbh>();
		doList = dataWgWgcjHjbhMapper.villageList(condition);
		return doList;
	}
	
	
	public DataWgWgcjHjbh getGridFamilySurvey(DataWgWgcjHjbh condition) {
		List<DataWgWgcjHjbh> doList = new ArrayList<DataWgWgcjHjbh>();
		doList = dataWgWgcjHjbhMapper.villageList(condition);
		if (doList.size() > 0) {
			return doList.get(0);
		} else {
			return new DataWgWgcjHjbh();
		}
	}
	
	public List<DataWgWgcj> gridFamilyList(DataWgWgcj dataWgWgcj) {
		List<DataWgWgcj> doList = new ArrayList<DataWgWgcj>();
		doList = dataWgWgcjMapper.getWgCjList(dataWgWgcj.getHjbh());
		return doList;
	}
	
	public List<DataWgWgcj> gridPeasant(DataWgWgcj dataWgWgcj, LoginUser userInfo,String ifCheck) {
		List<DataWgWgcj> doList = new ArrayList<DataWgWgcj>();
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office") || "1".equals(ifCheck)) {

		} else if (userInfo.getRoles().contains("zhhz")) {
			if ("0".equals(ifCheck)) {
				dataWgWgcj.setOrgId(orgId);
			}
		} else if (userInfo.getRoles().contains("common")) {
			dataWgWgcj.setOrgId(orgId);
			dataWgWgcj.setManagerId(userId);
		} else {
			throw new CustomException("没有权限进行该操作!");
		}

		doList = dataWgWgcjMapper.gridPeasantList(dataWgWgcj);
		return doList;
	}
	
	
	@Transactional
	public AjaxResult editWgcjOrg(DataWgWgcj vo, LoginUser userInfo) {
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			try {
				////同步修改DATA_WG_WGCJ   的管户
				dataWgWgcjMapper.editWgcjOrg(vo);
				//同步修改DATA_WG_WGCJ_HJBH   的管户
				dataWgWgcjMapper.editWgcjhJBH(vo);
				//同步修改DATA_WG_WGCJ_VILLAGE   的管户
				dataWgWgcjMapper.editWgcjVillageOrg(vo);
				//同步修改REPORT_WGCJ_VILLAGE_LSB   的管户
				dataWgWgcjMapper.editWgcjLsb(vo);
				//同步修改REPORT_WGCJ_BPJM_LSB   的管户
				dataWgWgcjMapper.editWgcjBpLsb(vo);
			} catch (Exception e) {
				e.printStackTrace();
				throw new CustomException("修改网格村居成员信息异常！"+e.getMessage());
			}
		}else {
			return AjaxResult.error(ErrorCode.NoAuthorization);
		}
		return AjaxResult.success();
	}
	
	
	public AjaxResult delWgcj(DataWgWgcj vo, LoginUser userInfo) {
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			DataWgWgcj dataWgWgcj = new DataWgWgcj();
			dataWgWgcj.setId(vo.getId());
			dataWgWgcj.setDelFlag("1");
			dataWgWgcj.setUpdateTime(DateUtils.getNowDate());
			dataWgWgcjMapper.updateDataWgWgcj(dataWgWgcj);
			return AjaxResult.success();
		}else {
			return AjaxResult.error(ErrorCode.NoAuthorization);
		}
	}
	
	public AjaxResult editFamilyMember(DataWgJtcy vo) {
		DataWgJtcy dataWgJtcy = dataWgJtcyMapper.selectDataWgJtcyById(vo.getId());
		if (dataWgJtcy != null) {
			// 修改家庭成员
			vo.setUpdateTime(DateUtils.getNowDate());
			dataWgJtcyMapper.updateDataWgJtcy(vo);
		} else {
			throw new CustomException("家庭成员不存在！");
		}
		return AjaxResult.success();
	}
	
	/**
	 * 分户 可输入目标家庭的户籍编号，也可以自己为户主 type 1表示分户，2表示从该户籍中删除
	 * 
	 * @param vo
	 */
	public AjaxResult delFamilyMember(DataWgJtcy vo) {
		// 判断目标家庭中是否存在本人
		if ("1".equals(vo.getType())) {
			DataWgJtcy dataWgJtcy = dataWgJtcyMapper.selectOne(vo);
			if (dataWgJtcy != null) {
				throw new CustomException("家庭成员已存在！");
			} else {

				dataWgJtcyMapper.updateDataWgJtcy(vo);
			}
		} else if ("2".equals(vo.getType())) {
			DataWgJtcy dataWgJtcy = new DataWgJtcy();
			dataWgJtcy.setId(vo.getId());
			dataWgJtcy.setDelFlag("1");
			dataWgJtcyMapper.updateDataWgJtcy(dataWgJtcy);
		} else {
			throw new CustomException("操作类型异常！");
		}
		return AjaxResult.success();
	}
	

	public List<DataWgWgcj> wgcjAppBiYear(LoginUser userInfo) {

		String orgId = null;
		String userId = null;

		if (userInfo.getRoles().contains("zhhz")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();

		} else if (userInfo.getRoles().contains("common")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();
			userId = userInfo.getSysUser().getUserName();
		}
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String twoYearAgo = DateUtilss.getLastYear(new Date(), -2);// 俩年前
		String ontYearAgo = DateUtilss.getLastYear(new Date(), -1);// 一年前
		String today = formatter.format(date).toString();
		List<DataWgWgcj> list = dataWgWgcjMapper.wgcjAppBiYear(twoYearAgo, ontYearAgo, today, orgId, userId);

		return list;
	}


	public List<DataWgWgcj> wgcjAppBiJd(LoginUser userInfo) {
		String orgId = null;
		String userId = null;

		if (userInfo.getRoles().contains("zhhz")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();

		} else if (userInfo.getRoles().contains("common")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();
			userId = userInfo.getSysUser().getUserName();
		}
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String fjd = DateUtilss.getLastDay(new Date(), -9);// 4季度
		String sjd = DateUtilss.getLastDay(new Date(), -6);// 3季度
		String tjd = DateUtilss.getLastDay(new Date(), -4);// 2季度 二季度减四
		String today = formatter.format(date).toString();
		List<DataWgWgcj> list = dataWgWgcjMapper.wgcjAppBiJd(fjd, sjd, tjd, today, orgId, userId);
		return list;
	}


	public List<DataWgWgcj> wgcjAppBiMonth(LoginUser userInfo) {
		String orgId = null;
		String userId = null;

		if (userInfo.getRoles().contains("zhhz")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();

		} else if (userInfo.getRoles().contains("common")) {
			orgId = userInfo.getSysUser().getDept().getOrgId();
			userId = userInfo.getSysUser().getUserName();
		}
		/**
		 * 获取今年内每个月最后一天
		 */
		ArrayList<String> sjrqs = DateUtilss.getEndMonthByYear(1, 0);

		List<DataWgWgcj> list = dataWgWgcjMapper.wgcjAppBiMonth(sjrqs, orgId, userId);
		return list;
	}

	@Override
	@Transactional
	public int addFamilyMember(DataWgJtcy vo, LoginUser userInfo) {
		int flag = 0;
		// 判断新增家庭成员是否存在
		DataWgJtcy dataWgJtcy = dataWgJtcyMapper.selectOne(vo);
		if (dataWgJtcy != null) {
			/**如果已存在，直接将客户设为当前家庭的成员*/
			dataWgJtcyMapper.updateDataWgJtcy(vo);
		} else {
			// 插入家庭成员信息表中
			dataWgJtcyMapper.insertDataWgJtcy(vo);

			// 如果村居中不存在，则新增
			List<DataWgWgcj> wgcj = dataWgWgcjMapper.queryWgCj(vo.getIdCard());
			if (wgcj.size() > 0) {
				logger.info("客户村居信息已存在！客户身份证号码=={}", vo.getIdCard());
			} else {
				String orgId = userInfo.getSysUser().getDept().getOrgId();
				String userId = userInfo.getSysUser().getUserName();

				// 新增网格村居信息
				DataWgWgcj dataWgWgcj = new DataWgWgcj();
				dataWgWgcj.setHjbh(vo.getHjbh());
				dataWgWgcj.setHzxm(vo.getHzxm());
				dataWgWgcj.setName(vo.getName());
				dataWgWgcj.setIdCard(vo.getIdCard());
				dataWgWgcj.setOrgId(orgId);
				dataWgWgcj.setManagerId(userId);
				dataWgWgcj.setYhzgx(vo.getYhzgx());

				dataWgWgcj.setAge(0);
				dataWgWgcj.setSex(vo.getSex());

				flag = dataWgWgcjMapper.insertDataWgWgcj(dataWgWgcj);
			}
		}

		return flag;
	}

	public AjaxResult cjTask(DataWgWgcj dataWgWgcj, LoginUser userInfo) {
		
		/**
		 * 先查询客户经理提交的查询条件能否查询到客户，如果没有查询结果直接返回
		 */

		String orgId = userInfo.getSysUser().getDept().getOrgId();
		String userId = userInfo.getSysUser().getUserName();

		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
			
		} else if (userInfo.getRoles().contains("zhhz")) {
			dataWgWgcj.setOrgId(orgId);
		} else if (userInfo.getRoles().contains("common")) {
			dataWgWgcj.setOrgId(orgId);
			dataWgWgcj.setManagerId(userId);
		}else {
			return AjaxResult.error(ErrorCode.NoAuthorization);
		}

		long count = dataWgWgcjMapper.gridPeasantCount(dataWgWgcj);

		if (count == 0) {
			return AjaxResult.error(ErrorCode.NotQueryResult);
		}

		/**
		 * 先保存方案
		 */
	    String planCode = GetOnlyId.getOnlyId();
        DataCustomQuery dataCustomQuery = new DataCustomQuery();
        dataCustomQuery.setPlanCode(planCode);
        dataCustomQuery.setPlanName(dataWgWgcj.getPlanName());
        dataCustomQuery.setCreateUser(userId);
        dataCustomQuery.setCreateTime(DateUtils.getNowDate());
        dataCustomQueryMapper.insertDataCustomQuery(dataCustomQuery);
//TODO
		DataTaskInfo dataTaskInfo = new DataTaskInfo();        
        dataTaskInfo.setTaskType("1");
        dataTaskInfo.setIfGroup("0");
        dataTaskInfo.setGroupCode(planCode);
			if (dataWgWgcj.getBeginDate() != null && dataWgWgcj.getEndDate() != null) {
				dataTaskInfo.setBeginDate(DateUtilss.beginOfDate(dataWgWgcj.getBeginDate()));// 设置任务开始时间
				dataTaskInfo.setEndDate(DateUtilss.endOfDate(dataWgWgcj.getEndDate())); // 设置任务结束时间
			} else {
				dataTaskInfo.setBeginDate(DateUtilss.beginOfDate(new Date()));// 设置任务开始时间
				dataTaskInfo.setEndDate(DateUtilss.endOfDate(new Date())); // 设置任务结束时间
			}


			dataTaskInfo.setTaskName(dataWgWgcj.getPlanName());
			dataTaskInfo.setTaskTarget("CK,DK,ETC,XYK,SJYH,WY,DXQY,SDDJ,SYB,POS");

			Map<String,String> code = dataTaskInfoService.insertTask(dataTaskInfo,userInfo);
			 if( code.get("taskId")!=null ) {
			/**
			 * 另启一个线程池 营销任务生成成功，则将查询方案和方案的客户进行关联 需要设置DATA_PLAN_TASK_RELATION status为0
			 * 不加入定时任务自动关联方案与客户
			 */
			fixedThreadPool.execute(() -> {
				try {
					final List<DataWgWgcj> list = dataWgWgcjMapper.gridList(dataWgWgcj);
					String taskId = code.get("taskId");

					DataPlanTaskRelation planTaskVO = new DataPlanTaskRelation();
                	planTaskVO.setPlanCode(planCode);
                	planTaskVO.setTaskId(taskId);

                	dataPlanTaskRelationMapper.updateDataPlanTaskRelation(planTaskVO);

                	List<DataUserTaskRelation> relationList = new ArrayList<DataUserTaskRelation>();
					// 关联方案与客户信息
					for (DataWgWgcj wgcj : list) {
						String relationId = GetOnlyId.getOnlyId();
                		DataUserTaskRelation userTaskRelationVO = new DataUserTaskRelation();
    					userTaskRelationVO.setId(relationId);
						userTaskRelationVO.setIdCard(wgcj.getIdCard());
						userTaskRelationVO.setOrgId(wgcj.getOrgId());
						userTaskRelationVO.setUserId(wgcj.getManagerId());
						userTaskRelationVO.setCusName(wgcj.getName());
						userTaskRelationVO.setTaskId(taskId);
    					userTaskRelationVO.setType("3");
    					userTaskRelationVO.setBeginDate(DateUtilss.beginOfDate(dataWgWgcj.getBeginDate()));
    					userTaskRelationVO.setEndDate(DateUtilss.endOfDate(dataWgWgcj.getEndDate()));
    					userTaskRelationVO.setStatus("1");
    					userTaskRelationVO.setComplete("0");
    					userTaskRelationVO.setCreateTime(DateUtils.getNowDate());
    					relationList.add(userTaskRelationVO);
    					if (relationList.size() == 1000) {
    						dataUserTaskRelationMapper.insertDataUserTaskRelationList(relationList);
    	                	relationList = new ArrayList<DataUserTaskRelation>();
    	                	//1000条数据提交一次
    					}
					}

					if (relationList.size() > 0) {
                		dataUserTaskRelationMapper.insertDataUserTaskRelationList(relationList);
	                	relationList = new ArrayList<DataUserTaskRelation>();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}

		return  AjaxResult.success();
	}
	
	public List<DataCustomerRecord> queryWgBg(DataCustomerRecord dataCustomerRecord,  LoginUser userInfo) {
		
		List<DataCustomerRecord> doList = new ArrayList<DataCustomerRecord>();
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
		} else {
			// 其他的暂时不返回数据
			throw new CustomException(String.valueOf(ErrorCode.NoAuthorization));
		}
		doList = dataCustomerRecordMapper.selectDataCustomerRecordList(dataCustomerRecord);
		return doList;
	}
	
	
	public List<DataKhhdImport> showKhhoInfo(DataKhhdImport dataKhhdImport,  LoginUser userInfo) {
		List<DataKhhdImport> doList = new ArrayList<DataKhhdImport>();
		if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
		} else {
			// 其他的暂时不返回数据
			throw new CustomException(String.valueOf(ErrorCode.NoAuthorization));
		}
		doList = dataKhhdImportMapper.selectDataKhhdImportList(dataKhhdImport);
		return doList;
	}
	
	@Transactional
	public AjaxResult convert(UserTaskRelationDO vo,  LoginUser userInfo) {
		String orgId = userInfo.getSysUser().getDept().getOrgId();
		
		Map<String, Object> map = new HashMap<String, Object>();
		// 目标管户
		map.put("newOrgId", vo.getOrgId());
		map.put("newUserId", vo.getUserId());
		//-------------目标OrgId:" + vo.getOrgId() + ",目标UserId:" + vo.getUserId());
		for (UserTaskRelationDO po : vo.getList()) {
			int type = Integer.valueOf(vo.getType());
			if (userInfo.getRoles().contains("admin") || userInfo.getRoles().contains("office")) {
				//管理员角色或者总行人员，修改所有客户 不做权限控制");
				// 管理员角色或者总行人员，修改所有客户 不做权限控制
			} else if (userInfo.getRoles().contains("zhhz")) {
				map.put("oldOrgId", orgId);
			} else {
				//非支行行长和管理员角色或者总行人员无权限转户");
				return AjaxResult.error(ErrorCode.NotQueryResults);
			}

			switch (type) {
			// 个体工商户
			case 2:
				//个体工商户ID:" + po.getId());
				if (po.getId() == null || "".equals(po.getId())) {
					return  AjaxResult.error(ErrorCode.IllegalArument);
				}
				// 个体工商户 个体工商户没有村组或社区一说 所以直接修改DATA_GTGSH表中所属管户
				map.put("id", String.valueOf(po.getId()));
				dataWgWgcjMapper.upShdateConvert(map);
				break;
			// 网格村居
			case 3:
				//网络村居TypeStatus:" + vo.getTypeStatus());
				// TypeStatus 是1 表示整村转户 是2 表示村中的某个人或多个人转户（或批量）
				if (vo.getTypeStatus() == 1) {
					//网络村居整村转户CusName:" + po.getCusName());
					if (po.getCusName() == null || "".equals(po.getCusName())) {
						return  AjaxResult.error(ErrorCode.IllegalArument);
					}
					map.put("village", String.valueOf(po.getCusName()));
					// 整村转户 传村名
					// 修改村居所属支行
//					gridMapper.upCjdateConvert(map);
					// 修改完村居所属支行后修改村居所属村名的管户
					dataWgWgcjMapper.upCjGrdateConvert(map);
				} else if (vo.getTypeStatus() == 2) {
					//网络村居个人或多人转户Id:" + po.getHjbh());
					if (po.getHjbh() == null || "".equals(po.getHjbh())) {
						return  AjaxResult.error(ErrorCode.IllegalArument);
					}
					// 个人或批量转户传 ID
					map.put("hjbh", String.valueOf(po.getHjbh()));
					dataWgWgcjMapper.upCjGrdateConvert(map);
				} else if (vo.getTypeStatus() == 3) {
					//网络村居个人或多人转户Id:" + po.getId());
					if (po.getId() == null || "".equals(po.getId())) {
						return  AjaxResult.error(ErrorCode.IllegalArument);
					}
					// 个人或批量转户传 ID
					map.put("id", String.valueOf(po.getId()));
					dataWgWgcjMapper.upCjGrdateConvert(map);
				}
				break;
			// 网格社区
			case 4:
				//网络社区TypeStatus:" + vo.getTypeStatus());
				// TypeStatus 是1 表示整个社区转户 是2表示 社区中某个人或多个人转户（或批量）
				if (vo.getTypeStatus() == 1) {
					//网格社区个人或多人转户CusName:" + po.getCusName());
					if (po.getCusName() == null || "".equals(po.getCusName())) {
						return AjaxResult.error(ErrorCode.IllegalArument);
					}
					// 整社区转户 传社区名
					map.put("sssq", String.valueOf(po.getCusName()));
					// 修改社区居所属支行
//					gridMapper.upSqdateConvert(map);
					// 修改完村居所属支行后修改村居所属村名的管户
					dataWgWgcjMapper.upSqGrdateConvert(map);
				} else if (vo.getTypeStatus() == 2) {
					//网络社区个人或多人转户Id:" + po.getId());
					if (po.getId() == null || "".equals(po.getId())) {
						return AjaxResult.error(ErrorCode.IllegalArument);
					}
					// 个人或批量转户传 ID
					map.put("id", String.valueOf(po.getId()));
					dataWgWgcjMapper.upSqGrdateConvert(map);
				}
				break;
			// 专业市场
			case 5:
				//专业市场整市转户TypeStatus:" + vo.getTypeStatus());
				// TypeStatus 是1 表示整个专业转户 是2表示 专业市场中某个人或多个人转户（或批量）
				if (vo.getTypeStatus() == 1) {
					//专业市场整市转户转户MarketId:" + po.getMarketId());
					if (po.getMarketId() == null || "".equals(po.getMarketId())) {
						return AjaxResult.error(ErrorCode.IllegalArument);
					}
					// 整村转户 传村名
					map.put("marketId", String.valueOf(po.getMarketId()));
					// 修改村居所属支行
					dataWgWgcjMapper.upZyscdateConvert(map);
					// 修改完村居所属支行后修改村居所属村名的管户
					dataWgWgcjMapper.upZyscGrdateConvert(map);
				} else if (vo.getTypeStatus() == 2) {
					//专业市场个人或多人转户Id:" + po.getId());
					if (po.getId() == null || "".equals(po.getId())) {
						return AjaxResult.error(ErrorCode.IllegalArument);
					}
					// 个人或批量转户传 ID
					map.put("id", String.valueOf(po.getId()));
					dataWgWgcjMapper.upZyscGrdateConvert(map);
				}

				break;
			}

		}
		return AjaxResult.success();
	}
	
	
	
	public List<DataWgWgcj> selectTownOrVillage(DataWgWgcj vo, LoginUser userInfo) {
		List<DataWgWgcj> list = new ArrayList<>();

		if ("1".equals(vo.getCjType())) {
			list = dataWgWgcjMapper.selectTown(vo);
		} else if ("2".equals(vo.getCjType())) {
			list = dataWgWgcjMapper.selectVillage(vo);
		} else if ("3".equals(vo.getCjType())) {
			list = dataWgWgcjMapper.selectTowns();
		}
		return list;
	}

	/**
	 * 查询网格村居
	 * 
	 * @param id 网格村居ID
	 * @return 网格村居
	 */
	@Override
	public DataWgWgcj selectDataWgWgcjById(String id) {
		return dataWgWgcjMapper.selectDataWgWgcjById(id);
	}

	/**
	 * 查询网格村居列表
	 * 
	 * @param dataWgWgcj 网格村居
	 * @return 网格村居
	 */
	@Override
	public List<DataWgWgcj> selectDataWgWgcjList(DataWgWgcj dataWgWgcj) {
		return dataWgWgcjMapper.selectDataWgWgcjList(dataWgWgcj);
	}

	/**
	 * 新增网格村居
	 * 
	 * @param dataWgWgcj 网格村居
	 * @return 结果
	 */
	@Override
	public int insertDataWgWgcj(DataWgWgcj dataWgWgcj) {
		dataWgWgcj.setCreateTime(DateUtils.getNowDate());
		return dataWgWgcjMapper.insertDataWgWgcj(dataWgWgcj);
	}

	/**
	 * 修改网格村居
	 * 
	 * @param dataWgWgcj 网格村居
	 * @return 结果
	 */
	@Override
	public int updateDataWgWgcj(DataWgWgcj dataWgWgcj) {
		dataWgWgcj.setUpdateTime(DateUtils.getNowDate());
		return dataWgWgcjMapper.updateDataWgWgcj(dataWgWgcj);
	}

	/**
	 * 批量删除网格村居
	 * 
	 * @param ids 需要删除的网格村居ID
	 * @return 结果
	 */
	@Override
	public int deleteDataWgWgcjByIds(String[] ids) {
		return dataWgWgcjMapper.deleteDataWgWgcjByIds(ids);
	}

	/**
	 * 删除网格村居信息
	 * 
	 * @param id 网格村居ID
	 * @return 结果
	 */
	@Override
	public int deleteDataWgWgcjById(String id) {
		return dataWgWgcjMapper.deleteDataWgWgcjById(id);
	}
}
